<?php
/**
 * Created by PhpStorm.
 * User: LuLingFeng
 * Date: 2017/8/2
 * Time: 13:08
 */

namespace App\Http\Admin\App\Controllers;

use App\Http\Admin\App\Models\Version;
use Framework\Extend\Helpers\Upload;
use Framework\BaseClass\Http\Admin\Controller;
class VersionController extends Controller
{
    /**
     * 获取APP版本信息列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function VersionList()
    {
        if (request()->isMethod('post')) {
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            if (!empty($search)) {
                foreach ($search as $k => $v) {
                    if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                }
            }

            $version = new Version();
            return $version->getVersionList($page, $pageSize, $searchField);
        } else {
            return $this->view('app-version-list');
        }
    }


    /**
     * 获取APP版本信息列表详情
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function VersionDetail()
    {
        try {
            $request = request()->all();
            $id = $request['id'];
            $version = new Version();
            $versionInfo = $version->getVersionInfo($id);
            return $this->view('app-version-detail')->with('versionInfo', $versionInfo);

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

    }



    /**
     * 编辑APP版本信息列表详情
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function VersionEdit()
    {
        try {
            if (request()->isMethod('post')) {
                $request = request()->all();
               // return $request;
                $rules = [
                    'name' => 'required',
                    'version' => 'required',
                    'url' => 'required',
                    'is_force_update' => 'required|numeric',
                    'is_current_version' => 'required|numeric',
                    'platform' => 'required',
                    'description' => 'required',
                ];
                $this->validateRequestParameters($request, $rules, true);
                $version = new Version();
                $re = $version->editVersion($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                $request = request()->all();
                $id = $request['id'];
                $version = new Version();
                $versionInfo = $version->editVersionInfo($id);
                return $this->view('app-version-edit')->with('versionInfo', $versionInfo);
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

    }

    public function addPage()
    {
        return $this->view('app-version-add');
    }

    public function add()
    {
        $data = request()->all();
        $rules = [
            'name'               => 'required',
            'version'            => 'required',
            'url'                => 'required',
            'is_force_update'    => 'required|numeric',
            'is_current_version' => 'required|numeric',
            'platform'           => 'required',
            'description'        => 'required',
        ];
        $this->validateRequestParameters($data, $rules, true);
        $version = new Version();
        $re = $version->addVersion($data);
        if (!$re) return $this->ajaxFail();
        return $this->ajaxSuccess();
    }

}